import { _decorator, Component } from 'cc';
import { core_model } from '../../core/model/core_model';
const { ccclass, property } = _decorator;

/**
 * 示例Model实现
 * 展示如何使用Model基类
 */
@ccclass('example_model')
export class example_model extends core_model {
    // 用户数据
    private _userName: string = '';
    private _userLevel: number = 1;
    private _userScore: number = 0;
    private _userCoins: number = 0;
    
    // 游戏设置
    private _soundEnabled: boolean = true;
    private _musicEnabled: boolean = true;
    private _difficulty: number = 1; // 1-简单, 2-中等, 3-困难
    
    // 游戏统计
    private _totalPlayTime: number = 0;
    private _gamesPlayed: number = 0;
    private _bestScore: number = 0;
    
    /**
     * 初始化数据
     */
    protected initializeData(): void {
        // 用户数据
        this.setData('userName', '');
        this.setData('userLevel', 1);
        this.setData('userScore', 0);
        this.setData('userCoins', 0);
        
        // 游戏设置
        this.setData('soundEnabled', true);
        this.setData('musicEnabled', true);
        this.setData('difficulty', 1);
        
        // 游戏统计
        this.setData('totalPlayTime', 0);
        this.setData('gamesPlayed', 0);
        this.setData('bestScore', 0);
        
        // 尝试从本地存储加载数据
        this.loadFromLocalStorage();
    }
    
    /**
     * 初始化完成后的回调
     */
    protected onInitialize(): void {
        console.log('ExampleModel初始化完成');
    }
    
    /**
     * 从本地存储加载数据
     */
    private loadFromLocalStorage(): void {
        try {
            // 模拟从本地存储加载数据
            const savedData = localStorage.getItem('example_game_data');
            if (savedData) {
                const data = JSON.parse(savedData);
                this.setDataBatch(data);
                console.log('从本地存储加载数据成功');
            }
        } catch (error) {
            console.error('从本地存储加载数据失败:', error);
        }
    }
    
    /**
     * 保存数据到本地存储
     */
    public saveToLocalStorage(): void {
        try {
            const data = this.getAllData();
            localStorage.setItem('example_game_data', JSON.stringify(data));
            console.log('数据保存到本地存储成功');
        } catch (error) {
            console.error('保存数据到本地存储失败:', error);
        }
    }
    
    /**
     * 重置所有数据
     */
    public resetAllData(): void {
        this.setDataBatch({
            userName: '',
            userLevel: 1,
            userScore: 0,
            userCoins: 0,
            soundEnabled: true,
            musicEnabled: true,
            difficulty: 1,
            totalPlayTime: 0,
            gamesPlayed: 0,
            bestScore: 0
        });
        console.log('所有数据已重置');
    }
    
    /**
     * 更新用户分数
     */
    public updateUserScore(score: number): void {
        const currentScore = this.getData<number>('userScore');
        const newScore = currentScore + score;
        this.setData('userScore', newScore);
        
        // 更新最佳分数
        const bestScore = this.getData<number>('bestScore');
        if (newScore > bestScore) {
            this.setData('bestScore', newScore);
        }
        
        // 检查是否升级
        this.checkLevelUp(newScore);
    }
    
    /**
     * 检查是否升级
     */
    private checkLevelUp(score: number): void {
        const currentLevel = this.getData<number>('userLevel');
        const requiredScore = currentLevel * 100;
        
        if (score >= requiredScore) {
            this.setData('userLevel', currentLevel + 1);
            console.log(`用户升级到等级 ${currentLevel + 1}`);
        }
    }
    
    /**
     * 增加游戏次数
     */
    public incrementGamesPlayed(): void {
        const currentCount = this.getData<number>('gamesPlayed');
        this.setData('gamesPlayed', currentCount + 1);
    }
    
    /**
     * 增加总游戏时间
     */
    public addPlayTime(seconds: number): void {
        const currentTime = this.getData<number>('totalPlayTime');
        this.setData('totalPlayTime', currentTime + seconds);
    }
    
    /**
     * 消费金币
     */
    public spendCoins(amount: number): boolean {
        const currentCoins = this.getData<number>('userCoins');
        if (currentCoins >= amount) {
            this.setData('userCoins', currentCoins - amount);
            return true;
        }
        return false;
    }
    
    /**
     * 获得金币
     */
    public earnCoins(amount: number): void {
        const currentCoins = this.getData<number>('userCoins');
        this.setData('userCoins', currentCoins + amount);
    }
    
    /**
     * 设置游戏难度
     */
    public setDifficulty(difficulty: number): void {
        if (difficulty >= 1 && difficulty <= 3) {
            this.setData('difficulty', difficulty);
        }
    }
    
    /**
     * 切换音效设置
     */
    public toggleSound(): void {
        const currentState = this.getData<boolean>('soundEnabled');
        this.setData('soundEnabled', !currentState);
    }
    
    /**
     * 切换音乐设置
     */
    public toggleMusic(): void {
        const currentState = this.getData<boolean>('musicEnabled');
        this.setData('musicEnabled', !currentState);
    }
    
    // 便捷方法
    public getUserName(): string {
        return this.getData<string>('userName');
    }
    
    public getUserLevel(): number {
        return this.getData<number>('userLevel');
    }
    
    public getUserScore(): number {
        return this.getData<number>('userScore');
    }
    
    public getUserCoins(): number {
        return this.getData<number>('userCoins');
    }
    
    public getBestScore(): number {
        return this.getData<number>('bestScore');
    }
    
    public getGamesPlayed(): number {
        return this.getData<number>('gamesPlayed');
    }
    
    public getTotalPlayTime(): number {
        return this.getData<number>('totalPlayTime');
    }
    
    public isSoundEnabled(): boolean {
        return this.getData<boolean>('soundEnabled');
    }
    
    public isMusicEnabled(): boolean {
        return this.getData<boolean>('musicEnabled');
    }
    
    public getDifficulty(): number {
        return this.getData<number>('difficulty');
    }
    
    /**
     * 销毁Model
     */
    onDestroy(): void {
        // 保存数据到本地存储
        this.saveToLocalStorage();
        super.onDestroy();
    }
}
